package net.zjitc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.zjitc.common.R;
import net.zjitc.dto.SetmealDto;
import net.zjitc.entiy.Category;
import net.zjitc.entiy.Setmeal;
import net.zjitc.entiy.SetmealDish;
import net.zjitc.service.CategoryService;
import net.zjitc.service.SetmealDishService;
import net.zjitc.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

//套餐管理
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;
    //新增套餐
    //请求 URL: http://localhost:8080/setmeal
    //请求方法: POST
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息:",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }
    //套餐分页查询
    //请求 URL: http://localhost:8080/setmeal/page?page=1&pageSize=10&name=11
    //请求方法: GET
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize ,String name){
        //分页构造器对象
        Page<Setmeal> pageInfo=new Page<>(page,pageSize);
        Page<SetmealDto> dtoPage=new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        //添加查询条件，根据name进行like模糊查询
        queryWrapper.like(name!=null,Setmeal::getName,name);
        //添加排序条件，根据更新时间降序排序
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Setmeal> records=pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item,setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查询分类对象
            Category category = categoryService.getById(categoryId);
            if(category != null){
                //分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        return R.success(dtoPage);

    }
    //删除套餐
    //请求 URL: http://localhost:8080/setmeal?ids=1651061984377303042
    //请求方法: DELETE
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }
    //停售起售
    //请求 URL: http://localhost:8080/setmeal/status/0?ids=1651061984377303042
    //请求方法: POST
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable("status") Integer status, @RequestParam("ids") List<Long> ids) {
        log.info("status:" + status + "/ids" + ids);
        //根据id去修改对应的状态
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(status);
            setmeal.setId(id);
            setmealService.updateById(setmeal);
        }

        return R.success("修改状态成功");
    }
    //填充表单
    //请求 URL: http://localhost:8080/setmeal/1651124094664278018
    //请求方法: GET
    @GetMapping("/{id}")
    public R<SetmealDto> findById(@PathVariable Long id) {
        log.info("填充表单:" + id);
        //需要一个SetmealDto
        Setmeal setmeal = setmealService.getById(id);
        //有一个setmeal    setmeal=>SetmealDto
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealDishes(list);
        return R.success(setmealDto);
    }
    //更新套餐
    //请求 URL: http://localhost:8080/setmeal
    //请求方法: PUT
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        log.info("更新setmealDto" + setmealDto);
        //第一步是更新套餐updateById

        //第二部是更新套餐-菜品关系表 先删除，再添加 这个逻辑和口味-菜品关系表是一样的
        setmealService.updateWithDish(setmealDto);
        return R.success("更新成功");
    }
    //根据分类id查询套餐
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

}
